import os

import yaml

from dotenv import load_dotenv, find_dotenv

from config.config import ENV_CONF_PATH

'''
读取器yaml文件
'''


class YamlReader:

    @staticmethod
    def yaml_reader(yaml_file_path: str) -> dict:
        try:
            with open(yaml_file_path, "r", encoding="utf-8") as file:
                data = yaml.safe_load(file)
                return data
        except FileNotFoundError:
            print("文件不存在！")
        except yaml.YAMLError as e:
            print(f"YAML 文件格式错误: {e}")

    @staticmethod
    def get_env_config() -> dict:
        '''
        获取环境配置
        :param:
        :return:
        '''
        load_env = load_dotenv(find_dotenv(), override=True)
        if load_env:
            env_path = os.getenv('ENV')
            file_path = os.path.join(ENV_CONF_PATH, env_path+'.yaml')
            return YamlReader.yaml_reader(file_path)
        else:
            raise ModuleNotFoundError('.env file not found')


if __name__ == "__main__":
    print(YamlReader.get_env_config())

